<?php 
namespace Admin\Controller;
use Think\Controller;
class ArtController extends CommonController {
	public function index(){
		$cate = D('Cat');
		$catelist = $cate->getTree();
		$this->assign('list',$catelist);
		$this->display();
	}
	public function add(){
		$data['title'] = I('title');
		$data['cat_id'] = I('cat_id');
		$data['excerpt'] = I('excerpt');
		$data['content'] = $_POST['content'];
		$data['seo_title'] = I('seo_title');
		$data['seo_keywords'] = I('seo_keywords');
		$data['seo_desc'] = I('seo_desc');
		$data['sort'] = I('sort');	
		$data['pubtime'] = I('pubtime');
		$data['authour'] = I('authour');
		$data['lastup'] = date('Y-m-d H:i:s');
		$data['recommended'] = I('recommended')?I('recommended'):0;
		$data['istop'] = I('istop')?I('istop'):0;
		if($_FILES['pic']['size']!=0){	
			$upload = new \Think\Upload();// 实例化上传类    
			$upload->maxSize   =     3145728 ;// 设置附件上传大小    
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
			$upload->savePath  =      'Uploads/'; // 设置附件上传目录    // 上传单个文件     
			$upload->rootPath='./';
			$info   =   $upload->uploadOne($_FILES['pic']);
			if(!$info) {// 上传错误提示错误信息        
				$this->error($upload->getError());    
			}else{
			// 上传成功 获取上传文件信息         
				$data['pic'] = $info['savepath'].$info['savename'];    
			}
		}
		$res = D('Art')->add($data);
		if($res){
            $this->success("添加成功",'',1);
        }else{
            $this->error("添加失败",'',1);
        }
	}
	public function delArt(){
		$art = M('Art');
		$where['art_id'] = I('art_id');
		$res = $art->where($where)->delete();
		if($res){
			$rs['status'] = 200;
		}else{
			$rs['status'] = 0;
		}
		$this->ajaxReturn($rs);
	}
	public function artlist(){
		$art = M('Art'); // 实例化User对象
		$count      = $art->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)

		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $art->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		//var_dump($list);exit;
		$this->display(); // 输出模板
	}
	public function artEdit(){
		$art = M('Art');
		$where['art_id'] = I('art_id');
		$res = $art->where($where)->find();
		$cate = D('Cat');
		$catelist = $cate->getTree();
		$this->assign('list',$catelist);
		$this->assign($res);
		//var_dump($res);exit;
		$this->display();
	}
	public function artUpdate(){
		$data['title'] = I('title');
		$data['cat_id'] = I('cat_id');
		$data['excerpt'] = I('excerpt');
		$data['content'] = $_POST['content'];
		$data['seo_title'] = I('seo_title');
		$data['seo_keywords'] = I('seo_keywords');
		$data['seo_desc'] = I('seo_desc');
		$data['sort'] = I('sort');	
		$data['pubtime'] = I('pubtime');
		$data['authour'] = I('authour');
		$data['lastup'] = date('Y-m-d H:i:s');
		$data['recommended'] = I('recommended')?I('recommended'):0;
		$data['istop'] = I('istop')?I('istop'):0;
		
		$where['art_id'] = I('art_id');
		if($_FILES['pic']['size']>0){	
			$upload = new \Think\Upload();// 实例化上传类    
			$upload->maxSize   =     3145728 ;// 设置附件上传大小    
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
			$upload->savePath  =      'Uploads/'; // 设置附件上传目录    // 上传单个文件     
			$upload->rootPath='./';
			$info   =   $upload->uploadOne($_FILES['pic']);
			if(!$info) {// 上传错误提示错误信息        
				$this->error($upload->getError());    
			}else{
			// 上传成功 获取上传文件信息         
				$data['pic'] = $info['savepath'].$info['savename'];    
			}
		}
		$res = D('Art')->where($where)->save($data);
		if($res){
            $this->success('更新成功','artlist',1);
        }else{
            $this->error('更新失败','',1);
        }
	}

}
?>